package com.qut.graduate.busrtquery.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.utils.DistanceUtil;
import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.interfaces.OnItemClickListener;

import java.util.List;

/**
 * 选择地点界面搜索是底部往上拉的控件里recyclerView的适配器
 */
public class SelectPlaceSlideAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "SelectPlaceSlideAdapter";
    private Context mContext;
    private LatLng startLatLng;
    private List<PoiInfo> mPoiSerachList;

    public SelectPlaceSlideAdapter(Context mContext, List<PoiInfo> mPoiSearchList, LatLng startLatLng) {
        this.mContext = mContext;
        this.startLatLng = startLatLng;
        this.mPoiSerachList = mPoiSearchList;
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener OnItemClickListener) {
        this.mOnItemClickListener = OnItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_place, parent, false);
        return new SelectPlaceSlideAdapter.SelectPlaceHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onClick(holder.itemView, position);
                }
            });
        }
        double dis = DistanceUtil.getDistance(startLatLng, mPoiSerachList.get(position).location);
        SelectPlaceSlideAdapter.SelectPlaceHolder selectPlaceHolder = (SelectPlaceSlideAdapter.SelectPlaceHolder) holder;
        selectPlaceHolder.mPlaceInfoName.setText(mPoiSerachList.get(position).name);
        String distance = "";
        if (dis < 1000) {
            distance = "[" + String.valueOf((int)dis) + " m]";
        } else {
            distance = "[" + String .format("%.2f",(dis / 1000)) + " km]";
        }
        selectPlaceHolder.mPlaceInfoDistance.setText(distance);
        selectPlaceHolder.mPlaceInfoAddress.setText(mPoiSerachList.get(position).address);
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return mPoiSerachList != null ? mPoiSerachList.size() : 0;
    }

    public class SelectPlaceHolder extends RecyclerView.ViewHolder {
        TextView mPlaceInfoName;
        TextView mPlaceInfoDistance;
        TextView mPlaceInfoAddress;

        public SelectPlaceHolder(View view) {
            super(view);
            mPlaceInfoName = view.findViewById(R.id.item_select_place_info_name);
            mPlaceInfoDistance = view.findViewById(R.id.item_select_place_info_dis);
            mPlaceInfoAddress = view.findViewById(R.id.item_select_place_info_address);
        }
    }

}
